<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Dashboard :: salesianichioggia.it</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="shortcut icon" href="http://www.salesianichioggia.it/img/favicon.ico">
        <!-- Le styles -->
        <link href="../bootstrap/css/bootstrap.css" rel="stylesheet" />
        <link href="../bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
        <link rel="stylesheet" href="js/uploadify/uploadify.css" />
        <style type="text/css">
            body {
                padding-top: 60px;
                padding-bottom: 40px;
                background: #f5f5f5 url('img/purty_wood.png') repeat;
            }
            .sidebar-nav {
                padding: 9px 0;
            }
            .table {
                background-color: #FFF;
                line-height: 100%;
            }
            tbody > tr > td:last-child {
                width: 10%;
            }

            .uploadify-button {
                color: #ffffff;
                text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
                background-color: #49afcd;
                *background-color: #2f96b4;
                background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#5bc0de), to(#2f96b4));
                background-image: -webkit-linear-gradient(top, #5bc0de, #2f96b4);
                background-image: -o-linear-gradient(top, #5bc0de, #2f96b4);
                background-image: linear-gradient(to bottom, #5bc0de, #2f96b4);
                background-image: -moz-linear-gradient(top, #5bc0de, #2f96b4);
                background-repeat: repeat-x;
                border-color: #2f96b4 #2f96b4 #1f6377;
                border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
                filter: progid:dximagetransform.microsoft.gradient(startColorstr='#ff5bc0de', endColorstr='#ff2f96b4', GradientType=0);
                filter: progid:dximagetransform.microsoft.gradient(enabled=false);
            }

            .uploadify-button:hover,
            .uploadify-button:active,
            .uploadify-button.active,
            .uploadify-button.disabled,
            .uploadify-button[disabled] {
                color: #ffffff;
                background-color: #2f96b4;
                *background-color: #2a85a0;
            }

            .uploadify-button:active,
            .uploadify-button.active {
                background-color: #24748c;
            }
            
            .thumb_box {
                min-height: 120px;
            }
            
            .btn_delete_img {
                position: absolute;
                margin-top: 5px;
                margin-left: 5px;
                display: none;
            }
        </style>


        <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
        <script src="js/uploadify/jquery.uploadify.min.js"></script>

        <script type="text/javascript">
            
            function btn_delete_img(btn) {
                if(confirm("Sei sicuro di voler eliminare la foto?")) {
                    $.post('http://www.salesianichioggia.it/dashboard/ajax/deleteimage.php', 
                                {
                                    id : $(btn).data('photoid')
                                }, 
                                function (data){
                                    if (data > 0) {
                                        $(btn).parent().parent().fadeOut('fast', function(){$(this).remove();});
                                    } else {
                                        alert("Non è ossibile eliminare la foto a causa di un errore sul server. Contatta i webmaster!");
                                    }
                                    
                                }
                            );
                }
            }
            
            function uploadify_onUploadSuccess(file, data, response) {
                var photo_info = jQuery.parseJSON(data);
                if($("#thumb_upload").length <= 0) {
                    $("#gallery_form").after('<ul class="thumbnails" id="thumb_upload"></ul>');
                    $("#gallery_form").after("<h2>Fotografie Caricate</h2>");
                }
                var li = $('<li class="span2"><div class="img_box_wrap"><button class="btn btn-danger btn-mini btn_delete_img" data-photoid="' + photo_info['id'] + '"><i class="icon-trash icon-white"></i> Elimina</button><img src="' + photo_info['url'] +'" class="img-polaroid" /></div></li>')
                $("#thumb_upload").append(li);
                li.find('.img_box_wrap').hover(function(){
                    $(this).find('.btn_delete_img').fadeToggle('fast');
                })
                li.find('.btn_delete_img').click(function(){
                    btn_delete_img(this);
                });
            }
            
            function update_folders_select() {
                $("#folder option").remove();
                $.ajax({
                    url : "http://www.salesianichioggia.it/dashboard/ajax/folders.php?action=list",
                    cache: false,
                    success: function(result) {
                        var folders = $.parseJSON(result);
                        $("#folder").append('<option value="root">Scegli una cartella...</option>');
                        for (var key in folders) {
                            $("#folder").append('<option value="' + key +'">' + folders[key] + '</option>');
                        }
                    }
                });
            }
            
            $(document).unload(function(){
                if (($("#title").val() != '') || ($("#description").val() != '') || ($("#date").val() != '') || ($("#folder-name").val() != '')) alert("Sei sicuro di voler abbandonare la pagina? Tutti i tuoi dati andranno persi...");
            });
            $(document).ready(function(){
                
                $("#date").datepicker({dateFormat:'dd/mm/yy',
                    dayNames: [ "Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdi", "Sabato" ],
                    monthNames: [ "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" ],
                    dayNamesMin: [ "Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab" ],
                    firstDay: 1
                });
                $("div.img_box_wrap").hover(function(){
                    $(this).find('.btn_delete_img').fadeToggle('fast');
                });
                $(".btn_delete_img").on('click', function(){
                    btn_delete_img(this);
                });
                
                $("a.delete").click(function(){
                   if (confirm("Sei sicuro di voler eliminare l'album selezionato?")) {
                       return true;
                   }
                   return false;
                });
                        <?php if (isset($gallery)) { ?>
                            $("div#gallery_info #file_upload").uploadify({
                                'buttonText' : '<i class="icon-plus icon-white"></i> Segli le foto',
                                'formData'     : {
                                    'timestamp' : '<?= $timestamp; ?>',
                                    'token'     : '<?= md5('unique_salt' . $timestamp); ?>',
                                    'year'      : '<?= $gallery->date->format('Y') ?>',
                                    'month'     : '<?= $gallery->date->format('m') ?>',
                                    'foldername': '<?= $gallery->path ?>',
                                    'gallery_id': '<?= $gallery->id ?>'
                                            
                                },
                                'swf' : 'js/uploadify/uploadify.swf',
                                'uploader' : 'http://www.salesianichioggia.it/dashboard/ajax/uploadify.php',
                                'onUploadError' : function(file, errorCode, errorMsg, errorString) {
                                    alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                                },
                                'onUploadSuccess' : uploadify_onUploadSuccess
                            });
                        <?php } ?>                                   
                        
                        $("#title").focus();
                        $("#btn_add_photos").click(function(e){
                    
                            var error = false;
                            if ($("#date").val() == '')
                            {
                                error = error || true;
                       
                                $("#date").parent().parent().addClass("error");
                                if ($("#date").siblings("span.help-inline").length == 0) {
                                    $("#date").after('<span class="help-inline">Campi Obbligatori!</span>');
                                }
                            }
                   
                            if($("#title").val() == '') {
                                error = error || true;
                                $("#title").parent().parent().addClass("error");
                                if ($("#title").siblings("span.help-inline").length == 0) {
                                    $("#title").after('<span class="help-inline">Campo Obbligatorio!</span>');
                                }
                            }
                    
                            if ($("#description").val() == '') {
                                error = error || true;
                                $("#description").parent().parent().addClass("error");
                                if ($("#description").siblings("span.help-inline").length == 0) {
                                    $("#description").after('<span class="help-inline">Campo Obbligatorio!</span>');
                                }
                            }
                            
                            if ($("#folder").val() == 'no-folder') {
                                error = error || true;
                                $("#folder").parent().parent().addClass("error");
                                if ($("#folder").siblings("span.help-inline").length == 0) {
                                    $("#folder").after('<span class="help-inline">Campo Obbligatorio!</span>');
                                }
                            }
                            
                            if ($("#folder-name").val() == '') {
                                error = error || true;
                                $("#folder-name").parent().parent().addClass("error");
                                if ($("#folder-name").siblings("span.help-inline").length == 0) {
                                    $("#folder-name").after('<span class="help-inline">Campo Obbligatorio!</span>');
                                }
                            }
                    
                            if(!error && confirm("Sei sicuro di voler proseguire? Le informazioni che hai inserito non saranno più modificabili!")) {
                        
                                var date_comp = $("#date").val().split("/");
                                var str_date = date_comp[2] + '-' + date_comp[1] + '-' + date_comp[0];
                                var folder_name = $("#folder-name").val();
                                var folder = $("#folder").val();
                                $.post('http://www.salesianichioggia.it/dashboard/ajax/gallery.php', 
                                {
                                    title : $("#title").val(), 
                                    description : $("#description").val(), 
                                    date : str_date, 
                                    foldername : folder_name,
                                    folder : folder,
                                    uid : <?= $u->id ?>
                                },
                                function(data) {
                                    <?php $timestamp = time(); ?>
                                    gallery_info = jQuery.parseJSON(data);
                                    $("input#gallery_id").val(gallery_info['gallery_id']);
                                    $("#file_upload").uploadify({
                                        'buttonText' : '<i class="icon-plus icon-white"></i> Segli le foto',
                                        'formData'     : {
                                            'timestamp' : '<?php echo $timestamp; ?>',
                                            'token'     : '<?php echo md5('unique_salt' . $timestamp); ?>',
                                            'year'      : gallery_info['year'],
                                            'month'     : gallery_info['month'],
                                            'foldername': gallery_info['foldername'],
                                            'gallery_id': gallery_info['gallery_id']

                                        },
                                        'swf' : 'js/uploadify/uploadify.swf',
                                        'uploader' : 'http://www.salesianichioggia.it/dashboard/ajax/uploadify.php',
                                        'onUploadError' : function(file, errorCode, errorMsg, errorString) {
                                            alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                                        },
                                        'onUploadSuccess' : uploadify_onUploadSuccess                                       
                                    });
                                    $("#gallery_info input").attr("disabled", "disabled");
                                    $('#gallery_id').removeAttr("disabled");
                                    var btn_save = $('<div class="control-group" id="submit_box" style="display:none;"><div class="controls"><button type="submit" class="btn btn-primary">Salva</button></div></div>');
                                    $("#btn_add_photos").slideUp('slow', function(){$(this).remove();$("#uploader").slideDown('slow');$('#gallery_info').append(btn_save);$("#submit_box").slideDown('slow');});
                                                
                             } // end function callback ajax
                             );
                        } //end if confirm

        });
                
        $("#folder-name, #name_folder").keypress(function(e){
                    
            if ((e.which >= 97 && e.which <= 122) || (e.which >= 48 && e.which <= 57) || (e.which == 45)) {
                        
                return true;
            }
                    
            return false;
        });
                
        $("#date, #title, #description, #folder-name, #name_folder, #folder, #description_folder").focus(function(){
            $(this).parent().parent().removeClass("error");
            $(this).parent().find("span.help-inline").remove(); 
        });
        
        
        
        $("#add_new_folder").click(function(){
            var modal = $('<div id="modal_add_folder" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>');
            var modal_header = $('<div class="modal-header"></div>');
            modal_header.append('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>');
            modal_header.append('<h3 id="myModalLabel">Aggiungi Cartella</h3>');

            var modal_body = $('<div class="modal-body"></div>');
            var modal_form = $('<form id="add_folder_form"></form>');
            modal_form.append('<div class="control-group"><label class="control-label" for="name_folder">ID</label><div class="controls"><input type="text" id="name_folder" placeholder="ID" name="name_folder" value="" /></div></div>');
            modal_form.append('<div class="control-group"><label class="control-label" for="description_folder">Descrizione</label><div class="controls"><input type="text" id="description_folder" placeholder="Descrizione" name="description_folder" value="" /></div></div>');
            var select = $('<select name="superfolder_folder" id="superfolder_folder"><option value="root">Radice</option></select>');
            $.ajax({
                url : 'http://www.salesianichioggia.it/dashboard/ajax/folders.php?action=list',
                cache: false,
                success: function(data) {
                    var folders = jQuery.parseJSON(data);
                    for (var key in folders) {
                       select.append('<option value="' + key + '">' + folders[key] + '</option>');
                    }
                }
            });
            
            var select_box = $('<div class="control-group"><label class="control-label" for="superfolder_folder">Cartella Genitore</label><div class="controls"></div></div>');
            select_box.find('.controls').append(select);
            modal_form.append(select_box);
            modal_body.append(modal_form);

            var modal_footer = $('<div class="modal-footer"></div>');
            modal_footer.append('<a class="btn" data-dismiss="modal" aria-hidden="true">Chiudi</a><a class="btn btn-primary" id="confirm">Aggiungi</a>');
            modal.append(modal_header, modal_body, modal_footer);
            var folder_name_exists = false;
            modal.find('#name_folder').focusout(function(){
                $.post("http://www.salesianichioggia.it/dashboard/ajax/folders.php?action=check", 
                  {name : $(this).val()},
                  function(data) {
                      var r = jQuery.parseJSON(data);
                      if (r['count'] > 0) {
                          folder_name_exists = true;
                          $("#name_folder").parent().parent().addClass("error");
                            if ($("#name_folder").siblings("span.help-inline").length == 0) {
                                $("#name_folder").after('<span class="help-inline">La cartella eiste già!</span>');
                            }
                      }  else {
                        folder_name_exists = false;
                      }
                  }
                );
            });
            
            modal_footer.find('#confirm').click(function(){
                var error = false;
                error = error || folder_name_exists;
                if ($("#name_folder").val() == '') {
                    error = error || true;
                    $("#name_folder").parent().parent().addClass("error");
                    if ($("#name_folder").siblings("span.help-inline").length == 0) {
                        $("#name_folder").after('<span class="help-inline">Campo Obbligatorio!</span>');
                    }
                }

                if ($("#description_folder").val() == '') {
                    error = error || true;
                    $("#description_folder").parent().parent().addClass("error");
                    if ($("#description_folder").siblings("span.help-inline").length == 0) {
                        $("#description_folder").after('<span class="help-inline">Campo Obbligatorio!</span>');
                    }
                }
                if (!error) {
                    $.post('http://www.salesianichioggia.it/dashboard/ajax/folders.php?action=save',
                           {
                               name : $("#name_folder").val(),
                               description : $("#description_folder").val(),
                               superfolder : $("#superfolder_folder").val()
                           },
                           function(data) {
                               var r = jQuery.parseJSON(data);
                               if(r['count'] <= 0) {
                                   alert('È avvenuto un errore nel server! Si prega di rieseguire l\'operazione!');   
                               }
                               modal.modal('hide');
                           }
                    );
                }
            });
            
            modal.on('hidden', function() {
                $(this).remove();
                update_folders_select();
            });
            
            modal.modal('show');
        });
    });
        </script>


        <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>
    <body>        
        <?= HTMLManager::get_dashboard_navigation(unserialize($_SESSION['user'])) ?>

        <div class="container-fluid">
            <div class="row-fluid">

                <?= HTMLManager::get_dashboard_activities(unserialize($_SESSION['user'])) ?>

                <div class="span9">
                    <?php
                    if ($_GET['action'] == ACTION_LIST) {
                        ?>
                        <h1>Gallerie Fotografiche del <?= $year ?></h1>
                        <a class="btn btn-primary" href="http://www.salesianichioggia.it/dashboard/gallery.php?action=list&year=<?= $year - 1 ?>">Anno Precendente</a> <a class="btn btn-primary" href="http://www.salesianichioggia.it/dashboard/gallery.php?action=list&year=<?= $year + 1 ?>">Anno Successivo</a>
                        <?php if (count($galleries) > 0) { ?>
                            <table class="table table-striped table-bordered" style="margin-top:10px;">
                                <thead>
                                    <tr>
                                        <td><strong>Titolo</strong></td>
                                        <td><strong>Descrizione</strong></td>
                                        <td><strong>Data</strong></td>
                                        <td><strong>Nome Cartella</strong></td>
                                        <td><strong>Azioni</strong></td>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach ($galleries as $gallery)
                                        $gallery->get_list_html();
                                    ?>
                                </tbody>
                            </table>
                        <?php } else {
                            ?>
                            <p>Non ci sono gallerie per questo anno!</p>
                            <?php
                        }
                    } else if ($_GET['action'] == ACTION_NEW) {
                        ?>
                        <form id="gallery_form" class="form-horizontal" method="POST" action="http://www.salesianichioggia.it/dashboard/gallery.php?action=save">
                            <legend>Nuova Galleria Fotografica</legend>
                            <div id="gallery_info">
                                <div class="control-group">
                                    <label class="control-label" for="title">Titolo</label>
                                    <div class="controls">
                                        <input  class="span12" type="text" id="title" placeholder="Titolo" name="title" value="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="description">Descrizione</label>
                                    <div class="controls">
                                        <input class="span12" type="text" id="description" placeholder="Descrizione" name="description" value="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="folder-name">Nome Cartella</label>
                                    <div class="controls">
                                        <input class="span12" type="text" id="folder-name" placeholder="Nome Cartella" name="folder-name" value="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="date">Data</label>
                                    <div class="controls">
                                        <input class="span3" type="text" name ="date" id="date" placeholder="20/10/2012" value="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="date">Cartella</label>
                                    <div class="controls">
                                        <select name="folder" id="folder">
                                            <option value="no-folder">Scegli una cartella...</option>
                                            <?php
                                            $folders = $gm->get_folders();
                                            foreach ($folders as $name => $description) { ?>
                                                <option value="<?= $name ?>"><?= $description ?></option>
                                            <?php    
                                            }
                                            ?>
                                        </select>
                                        <a class="btn btn-primary" data-toggle="modal" data-target="#modal_add_folder" id="add_new_folder"><i class="icon-plus icon-white"></i>Aggiungi cartella</a>
                                    </div>
                                </div>
                                <div class="control-group" style="display:none;">
                                    <div class="controls">
                                        <input class="span3" type="hidden" name ="gallery_id" id="gallery_id" value="" />
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <a class="btn btn-success" id="btn_add_photos"><i class="icon-plus icon-white"></i> Aggiungi Foto</a>
                                    </div>
                                </div>
                            </div>
                            <div id="uploader" style="display:none;">
                                <div class="control-group">
                                    <div class="controls">
                                        <input type="file" name="file_upload" id="file_upload" />
                                    </div>
                                </div>
                            </div>
                    </div>
                    </form>
                <?php } else if ($_GET['action'] == ACTION_EDIT) { ?>

                            <form id="gallery_form" class="form-horizontal" method="POST" action="http://www.salesianichioggia.it/dashboard/gallery.php?action=save&id=<?= $gallery->id ?>">
                        <legend>Modifica Galleria Fotografica</legend>
                        <div id="gallery_info">
                            <div class="control-group">
                                <label class="control-label" for="title">Titolo</label>
                                <div class="controls">
                                    <input  class="span12" type="text" id="title" placeholder="Titolo" name="title" value="<?= $gallery->title ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="description">Descrizione</label>
                                <div class="controls">
                                    <input class="span12" type="text" id="description" placeholder="Descrizione" name="description" value="<?= $gallery->description ?>" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="folder-name">Nome Cartella</label>
                                <div class="controls">
                                    <input class="span12" type="text" id="folder-name" placeholder="Nome Cartella" name="folder-name" value="<?= $gallery->path ?>" disabled="disabled" />
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="date">Data</label>
                                <div class="controls">
                                    <input class="span3" type="text" name ="date" id="date" placeholder="20/10/2012" value="<?= $gallery->date->format('d/m/Y') ?>" disabled="disabled" />
                                </div>
                            </div>
                            <div class="control-group">
                                    <label class="control-label" for="date">Cartella</label>
                                    <div class="controls">
                                        <select name="folder" id="folder">
                                            <option value="no-folder">Scegli una cartella...</option>
                                            <?php
                                            $folders = $gm->get_folders();
                                            foreach ($folders as $name => $description) { ?>
                                                <option value="<?= $name ?>" <?= ($gallery->folder == $name) ? 'selected' : '' ?>><?= $description ?></option>
                                            <?php    
                                            }
                                            ?>
                                        </select>
                                        <a class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="add_new_folder"><i class="icon-plus icon-white"></i>Aggiungi cartella</a>
                                    </div>
                                </div>
                            <div id="uploader">
                                <div class="control-group">
                                    <div class="controls">
                                        <input type="file" name="file_upload" id="file_upload" />
                                    </div>
                                </div>
                            </div>
                            <div class="control-group" id="submit_box">
                                <div class="controls">
                                    <button type="submit" class="btn btn-primary">Salva</button>
                                </div>
                            </div>
                        </div>
            </form>
            
            <?php 
                if (count($gallery->photos) > 0) { ?>
                    <h2>Fotografie Caricate</h2>
                            <ul class="thumbnails" id="thumb_upload">
                                <?php
                                    foreach ($gallery->photos as $photo) { ?>
                                <li class="span2">
                                    <div class="img_box_wrap">
                                        <button data-photoid="<?= $photo->id ?>" class="btn btn-danger btn-mini btn_delete_img"><i class="icon-trash icon-white"></i> Elimina</button>
                                        <img src="<?= $gallery->get_thumbnails_path() . $photo->url ?>" class="img-polaroid" />
                                    </div>
                                </li>
                                   <?php 
                                   }
                                ?>
                            </ul>
                <?php }
            ?>
                           
        <?php }
        ?>
    </div>
</div>
</div>
<script src="../bootstrap/js/bootstrap.js"></script>
<script src="../bootstrap/js/bootstrap-transition.js"></script>
</body>
</html>
